<?php
	$_title='个人主页';
	include_once 'head.php';
	$user_fans="user_fans";				//会员粉丝标
	$infoc=new info();
	$userc=new user();
	$newc=new news();
	$type=$_GET['type'];				//作者类型：小编vs会员
	$admin_id=$_GET['id']==''?$user_id:$_GET['id'];				//传过来的作者ID

	//判断是否为当前用户
	if($user_id==$admin_id&&$type=='user'){
			$user_type='own';			//自己查看自己
	}else{
		if($admin_id!='' && $admin_id!=null){
			$user_type='other';			//别人查看自己
		}	
	}

	//添加、取消关注
	if($_GET['action']=="addFocus"){
		$isfocus=$_POST['isfocus'];			//关注、取消
		$types=$_POST['types'];				//关注类型
		$arr=array('user_id'=>$admin_id,'type'=>$types,'fans_id'=>$user_id,'create_time'=>time());
		if($user_id!=''){
			if($isfocus==''){
				$error=$commonc->insert($user_fans,$arr);
				$id=$commonc->insert_id();
				$title="不关注";
				$msg='关注成功！';
			}else{
				$error=$commonc->del($user_fans,$isfocus.',');
				$title="关注TA";
				$msg='关注已取消！';
			}
		}else{
			$error=2;
			$msg="您还未登录，请先登录！";
		}
		$result=array('error'=>$error,'msg'=>$msg,'title'=>$title,'id'=>$id);
		echo json_encode($result);
		exit();
	}
	
	//发送私信
	if($_GET['action']=='sendLetter'){
		if($user_id!=''){
			$content=$_POST['content'];
			$to_user=$_POST['to_user'];
			$arr=array('from_user'=>$user_id,
						'to_user'=>$to_user,
						'content'=>$content,
						'create_time'=>time(),
					);
			$res=$newc->send_letter($arr);
			if($res>0){
				$error=0;
				$msg="发送成功！";
			}else{
				$error=2;
				$msg="发送失败，请重试！";
			}
		}else{
			$error=2;
			$msg="您还未登录，请先登录！";
		}
		$result=array('error'=>$error,'msg'=>$msg);
		echo json_encode($result);
		exit();
	}


	//读取作者个人信息
	if($admin_id!='' && $admin_id!=null){
		$wheres=' AND id="'.$admin_id.'"';
		$key=$type=='user'?'a.*':'a.*,a.name as account,a.intro as self_intro';
		$user=$infoc->adminInfo($type,$wheres,$key);
		$smarty->assign('user',$user);
	}	



	//文章是否通过审核
	$check=$_GET['check']!=''?$_GET['check']:'pass';
	$smarty->assign('check',$check);

	//读取咨询
	$where=" AND i.user_id='".$admin_id."' AND i.type='".$type."' 
		     AND i.check='".$check."' ORDER BY i.create_time desc";
	$key='i.*';
	$num='4';
	$row=$infoc->infoList($where,$key,$num);
	$page=$row['page'];
	$row=$row['result'];

	//关注了
	if($type=='user'){
		$where=' AND c.fans_id="'.$admin_id.'"';
		$gz=$userc->user_fans_num($where);	
	}else{
		$gz=0;
	}
	$smarty->assign('gz',$gz);

	//被关注
	$isfocus='';							//是否被当前登录的会员关注
	$where=' AND c.user_id="'.$admin_id.'" AND c.type="'.$type.'" ';
	$bgz=$userc->userFans($where,'c.*');
	if(is_array($bgz['result'])){
		foreach($bgz['result'] as $k=>$v){
			if($user_id==$v['fans_id']){
				$isfocus=$v['id'];			//被关注表的id
			}
		}
	}
	$bgz=count($bgz['result']);
	$smarty->assign('bgz',$bgz);
	$smarty->assign('isfocus',$isfocus);

	$smarty->assign('page',$page);
	$smarty->assign('row',$row);

	//html模板区分
	$html=$user_type=='own'?'user_main.html':'user_main_other.html';
	$smarty->display($html);		
?>